<template>
  <div>
    <a-table
      style="width: 100%; overflow-x: auto"
      v-if="columns.length"
      ref="tableList"
      @mouseleave.native="onMouseLeaveTable"
      :columns="columns"
      :data-source="dataSource"
      :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
      :row-key="(record) => record.id"
      :pagination="{
        current: pageNo,
        total: this.totalCount,
        showSizeChanger: true,
        onChange: this.tablePageChange,
        onShowSizeChange: this.paginationSizeChange,
      }"
      :loading="loading"
      :customRow="
        (record, index) => {
          return {
            on: {
              mouseenter: (e) => {
                this.currentRowId = record.id
              },
            },
          }
        }
      "
    >
      <!-- 产品名称 -->
      <template slot="goods_name" slot-scope="text, goods">
        <div class="goods-info">
          <img class="goods-pic" :src="goods.cover_pic" alt="" />
          <div class="info-box">
            <a-tooltip placement="top">
              <template slot="title">
                <span class="goods-name">{{ goods.name }} </span>
              </template>
              <div style="font-weight: bold; line-height: 25px" class="of_el">
                {{ goods.name }}
              </div>
            </a-tooltip>
            <div style="color: rgb(140, 140, 140)" class="of_el">
              {{ goods.cat ? goods.cat.map((item) => item.name).join() : '暂无分类' }}
            </div>
            <a-tooltip placement="top">
              <template slot="title"> 供货商：{{ goods.supplier_name }} </template>
              <span class="goods-name">供货商：{{ goods.supplier_name }} </span>
            </a-tooltip>
          </div>
          <div class="item-edit-box">
            <a-icon
              class="table_operator_icon"
              size="small"
              v-show="currentRowId == goods.id || goods.visible0"
              type="edit"
              @click="openPopover(goods, 0, $event)"
              title="编辑"
            />
            <MyPopover
              :title="'产品名称'"
              v-if="currentGoodsItem.id == goods.id"
              @onSubmit="submitUpdate(goods, 0)"
              :visible.sync="showNamePopover"
              :isHarf="currentGoodsItem.isHarf"
            >
              <a-textarea placeholder="请输入商品名称" v-model="updateParams.name" style="width: 250px" />
            </MyPopover>
          </div>
        </div>
      </template>
      <template slot="supplier" slot-scope="text, goods">
        <div class="goods-info">
          <img class="goods-pic" :src="goods.supplier_logo" alt="" />
          <div class="info-box">
            <a-tooltip placement="top">
              <template slot="title">
                <span class="goods-name">{{ goods.supplier_name }} </span>
              </template>
              <div style="font-weight: bold; line-height: 25px" class="of_el">
                {{ goods.supplier_name }}
              </div>
            </a-tooltip>
            <span class="goods-name">{{ goods.supplier_phone }} </span>
          </div>
        </div>
      </template>

      <!-- <template slot="supplier" slot-scope="text, goods">
        <div class="goods-info">
          <img class="goods-pic" :src="goods.supplier_logo" alt="" />
          <div class="info-box">
            <a-tooltip placement="top">
              <template slot="title">
                <span class="goods-name">{{ goods.supplier_name }} </span>
              </template>
              <div style="font-weight: bold; line-height: 25px; width: 160px" class="of_el">
                {{ goods.supplier_name }}
              </div>
            </a-tooltip>
            <span class="goods-name">{{ goods.supplier_phone }} </span>
          </div>
        </div>
      </template> -->

      <!-- 产品类型 -->
      <template slot="product_type" slot-scope="text">
        <span class="goods-type" v-if="text == 0">普通商品</span>
        <span class="goods-type" v-if="text == 1">酒店预约</span>
        <span class="goods-type" v-if="text == 2">服务预约</span>
        <span class="goods-type" v-if="text == 3">虚拟商品</span>
        <span class="goods-type" v-if="text == 4">积分商品</span>
      </template>
      <!-- 售价 -->
      <template slot="price" slot-scope="text, goods">
        <div class="goods-price-row">
          <span>{{ text }}</span>
          <div v-if="goods.product_type == 0" class="row-price-edit">
            <div style="position: relative">
              <a-icon
                class="table_operator_icon"
                size="small"
                type="edit"
                title="编辑"
                v-show="currentRowId == goods.id || goods.visible1"
                @click="setAttr(goods.id, goods.attr, goods, 1)"
              />
              <MyPopover
                :title="'售价'"
                @onSubmit="submitUpdate(goods, 1)"
                v-if="currentGoodsItem.id == goods.id"
                :visible.sync="showPricePopover"
                :isHarf="goods.isHarf"
                :showBtn="false"
              >
                <!-- 有规格时插槽 -->
                <!-- goods.use_attr == 1 -->
                <div v-if="false">
                  <a-table
                    v-if="attr.length > 0"
                    ref="table"
                    :columns="attrColumns"
                    class="slotTable"
                    :style="{ marginTop: '10px', maxHeight: '120px', overflowY: 'scroll', minHeight: '0px' }"
                    bordered
                    :dataSource="attr"
                    size="small"
                    :row-key="(r, i) => JSON.stringify(r)"
                    :pagination="false"
                  >
                    <div slot="priceDropdown" slot-scope="setSelectedKeys, selectedKeys" style="padding: 8px">
                      <a-input-search
                        @search="(e) => settingAll(e, setSelectedKeys, selectedKeys)"
                        size="small"
                        placeholder="批量设置"
                      >
                        <a-icon slot="enterButton" type="plus" />
                      </a-input-search>
                    </div>
                    <span slot="attr_list" slot-scope="attr_list">
                      <span style="margin-right: 4px" v-for="(item, index) of attr_list" :key="index">
                        {{ item.attr_name }}
                      </span>
                    </span>
                    <template slot="price" slot-scope="price, attrItem, attrIndex">
                      <a-input-number
                        style="display: inherit"
                        size="small"
                        :autoFocus="autoFocusData.type == 'price' && autoFocusData.index == attrIndex"
                        :value="price"
                        @change="(e) => handleChange(e, 'price', attrIndex)"
                      />
                    </template>
                  </a-table>
                  <div style="margin-top: 10px; float: right">
                    <a-button @click="hide(goods, 1)">取消</a-button>
                    <a-button type="primary" @click="handleOk(goods)" style="margin-left: 10px">确认</a-button>
                  </div>
                  <div style="clear: both"></div>
                </div>
                <!-- 无规格时插槽 -->
                <div v-else>
                  <a-input-number style="width: 100%" :min="0" :step="0.01" v-model="updateParams.price" />
                  <div style="margin-top: 10px; float: right">
                    <a-button @click="resetPopover">取消</a-button>
                    <a-button type="primary" @click="submitUpdate(goods, 1)" style="margin-left: 10px">确认</a-button>
                  </div>
                  <div style="clear: both"></div>
                </div>
              </MyPopover>
            </div>
          </div>
        </div>
      </template>
      <!-- 库存 -->
      <template slot="goods_num" slot-scope="text, goods">
        <div class="goods-rest-row">
          <span>{{ text }}</span>
          <div v-if="goods.product_type == 0" class="row-rest-edit">
            <div style="position: relative">
              <a-icon
                class="table_operator_icon"
                size="small"
                type="edit"
                title="编辑"
                v-show="currentRowId == goods.id || goods.visible2"
                @click="setAttr(goods.id, goods.attr, goods, 2)"
              />
            </div>
          </div>
          <MyPopover
            :title="'库存'"
            @onSubmit="submitUpdate(goods, 2)"
            v-if="currentGoodsItem.id == goods.id"
            :visible.sync="showRestPopover"
            :isHarf="goods.isHarf"
            :showBtn="false"
            :isAttr="goods.use_attr"
            :style="goods.use_attr == 1 ? { transform: 'translateX(-65px)', padding: '5px 20px' } : {}"
          >
            <!-- 有规格时插槽 -->
            <!-- goods.use_attr == 1 -->
            <div v-if="goods.use_attr == 1">
              <a-table
                v-if="attr.length > 0"
                ref="table"
                class="slotTable"
                :columns="attrColumns"
                :style="{ marginTop: '5px', maxHeight: '90px', overflowY: 'scroll', minHeight: '0px' }"
                bordered
                :dataSource="attr"
                size="small"
                :row-key="(r, i) => JSON.stringify(r)"
                :pagination="false"
              >
                <div slot="numDropdown" slot-scope="setSelectedKeys, selectedKeys" style="padding: 8px">
                  <a-input-search
                    @search="(e) => settingAll(e, setSelectedKeys, selectedKeys)"
                    size="small"
                    placeholder="批量设置"
                  >
                    <a-icon slot="enterButton" type="plus" />
                  </a-input-search>
                </div>
                <span slot="attr_list" slot-scope="attr_list">
                  <span style="margin-right: 4px" v-for="(item, index) of attr_list" :key="index">
                    {{ item.attr_name }}
                  </span>
                </span>
                <template slot="num" slot-scope="num, attrItem, attrIndex">
                  <a-input-number
                    style="display: inherit"
                    size="small"
                    :autoFocus="autoFocusData.type == 'num' && autoFocusData.index == attrIndex"
                    :value="num"
                    @change="(e) => handleChange(e, 'num', attrIndex)"
                  />
                </template>
              </a-table>
              <div style="margin-top: 10px; float: right">
                <a-button @click="hide(goods, 2)">取消</a-button>
                <a-button type="primary" @click="handleOk(goods)" style="margin-left: 10px">确认</a-button>
              </div>
              <div style="clear: both"></div>
            </div>
            <!-- 无规格时插槽 -->
            <div v-else>
              <a-input-number style="width: 100%" :min="0" :step="1" v-model="updateParams.goods_num" />
              <div style="margin-top: 10px; float: right">
                <a-button @click="resetPopover">取消</a-button>
                <a-button type="primary" @click="submitUpdate(goods, 2)" style="margin-left: 10px">确认</a-button>
              </div>
              <div style="clear: both"></div>
            </div>
          </MyPopover>
        </div>
      </template>
      <!-- 虚拟销量 -->
      <template slot="virtual_sales" slot-scope="text, goods">
        <div class="goods-virtual-row">
          <span>{{ text }}</span>
          <div class="row-virtual-edit">
            <div style="position: relative">
              <a-icon
                class="table_operator_icon"
                size="small"
                v-show="currentRowId == goods.id"
                type="edit"
                @click="openPopover(goods, 4, $event)"
                title="编辑"
              />
              <MyPopover
                :title="'虚拟销量'"
                @onSubmit="submitUpdate(goods, 4)"
                v-if="currentGoodsItem.id == goods.id"
                :visible.sync="showVirtualPopover"
                :isHarf="goods.isHarf"
              >
                <a-input-number style="width: 100%" :min="0" v-model="updateParams.virtual_sales" />
              </MyPopover>
            </div>
          </div>
        </div>
      </template>
      <template slot="profit" slot-scope="text, goods">
        ￥{{ parseFloat(goods.price) - parseFloat(goods.cost_price) }}
      </template>
      <template slot="sort" slot-scope="text, goods">
        <div class="goods-virtual-row">
          <span>{{ text }}</span>
          <div class="row-virtual-edit">
            <div style="position: relative">
              <a-icon
                class="table_operator_icon"
                size="small"
                v-show="currentRowId == goods.id"
                type="edit"
                @click="openPopover(goods, 5, $event)"
                title="编辑"
              />
              <MyPopover
                :title="'排序'"
                @onSubmit="submitUpdate(goods, 5)"
                v-if="currentGoodsItem.id == goods.id"
                :visible.sync="showSortPopover"
                :isHarf="goods.isHarf"
              >
                <a-input-number style="width: 100%" :min="0" v-model="updateParams.sort" />
              </MyPopover>
            </div>
          </div>
        </div>
      </template>
      <!-- 上下架 -->
      <a-switch
        slot="status"
        checked-children="上架"
        un-checked-children="下架"
        slot-scope="status, goods"
        :checked="status > 0"
        @change="onConfirmStatus('status', goods.id)"
      />
      <!-- 操作 -->
      <span class="table-action" slot="action" slot-scope="text, goods">
        <a @click="onEdit(goods.id)">编辑</a>
        <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
          <a-menu slot="overlay">
            <!-- <a-menu-item>
              <a href="javascript:;" @click="synchronizationGood(goods.id)">同步门店商品</a>
            </a-menu-item> -->
            <a-menu-item v-show="goods.use_attr == 1">
              <a href="javascript:;" @click="editAttr(goods)">编辑规格</a>
            </a-menu-item>
            <!-- <a-menu-item v-if="goods.product_type == 0">
              <a href="javascript:;" @click="openDrawer(goods.id, goods)">满减设置</a>
            </a-menu-item> -->
            <a-menu-item>
              <a href="javascript:;" @click="updateProduct(goods.cloud_goods_id)">更新商品</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;" @click="showQscode(goods.id)">小程序二维码</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;" @click="copyUrl(goods.id)">复制小程序链接</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;" @click="delGoodById(goods.id)">删除</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </a-table>

    <!-- 编辑规格弹框 -->
    <EditAttrModal
      :visible="isShowEditAttrModal"
      :currentGood="currentGood"
      @onAfterClose="onAfterClose"
      @onHandleOk="onAfterClose"
    ></EditAttrModal>
    <a-modal title="小程序二维码" :visible="showQrCode" @ok="showQrCode = false" @cancel="showQrCode = false">
      <div style="display: flex; justify-content: center">
        <img style="width: 300px; height: 300px" :src="this.qrcodeUrl" alt="" />
      </div>
    </a-modal>

    <!-- 绑定商品的弹窗 -->
    <a-modal title="商品列表" width="1000px" okText="确认导入" v-model="showBind" @ok="leadGood" :destroyOnClose="true">
      <a-table
        :columns="skuColumns"
        :data-source="currentCloud.attrs"
        bordered
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="false"
      >
        <div slot="attr_list" slot-scope="text, goods">
          {{ text.map((el) => el.attr_name).join('-') }}
        </div>
        <div slot="pic" slot-scope="text">
          <img :src="text" alt="" style="width: 48px; height: 48px; border-radius: 2px" />
        </div>
        <div slot="no" slot-scope="text">
          <a-tooltip>
            <template slot="title">
              {{ text }}
            </template>
            <span>{{ text }}</span>
          </a-tooltip>
        </div>
        <span slot="price" slot-scope="text"> ￥{{ text || 0 }} </span>
      </a-table>
    </a-modal>
  </div>
</template>

<script>
import {
  list,
  status,
  updateAll as updateAllApi,
  setAttrNum,
  setAttr,
  getQr,
  synchronizationOutlet,
} from '@/api/goods/goods'
import EditAttrModal from './EditAttrModal'
import MyPopover from './MyPopover'
import { supplierGoods } from '@/api/purchase/purchase'
import { leadGood } from '@/api/purchase/business'
const skuColumns = [
  {
    title: '规格名称',
    dataIndex: 'attr_list',
    scopedSlots: {
      customRender: 'attr_list',
    },
  },
  {
    title: '成本价',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price',
    },
    sorter: (a, b) => a.price - b.price,
  },
  {
    title: '售价',
    dataIndex: 'sale_price',
    scopedSlots: {
      customRender: 'sale_price',
    },
    sorter: (a, b) => a.sale_price - b.sale_price,
  },
  {
    title: '库存',
    dataIndex: 'num',
    sorter: (a, b) => a.num - b.num,
  },

  {
    title: '规格图片',
    dataIndex: 'pic',
    scopedSlots: {
      customRender: 'pic',
    },
  },
  {
    title: '规格货号',
    dataIndex: 'no',
    width: 100,
    scopedSlots: {
      customRender: 'no',
    },
  },
]

const attrColumns = [
  {
    title: '规格',
    dataIndex: 'attr_list',
    width: 200,
    scopedSlots: { customRender: 'attr_list' },
  },
  {
    title: '库存',
    dataIndex: 'num',
    width: 80,
    scopedSlots: {
      filterDropdown: 'numDropdown',
      filterIcon: 'numIcon',
      customRender: 'num',
    },
  },
  {
    title: '价格',
    dataIndex: 'price',
    width: 80,
    scopedSlots: {
      filterDropdown: 'priceDropdown',
      filterIcon: 'priceIcon',
      customRender: 'price',
    },
  },
]

export default {
  components: {
    MyPopover,
    EditAttrModal,
  },
  props: {
    type: {
      type: String,
      default: '',
    },
    status: {
      type: Number,
      default: -1,
    },
    queryParam: {
      type: Object,
      default: () => {
        return {
          name: '',
          cat_id: 0,
          status: -1,
          mch_id: 0,
        }
      },
    },
  },
  data() {
    return {
      isShowEditAttrModal: false,
      currentRowId: '',
      goodsId: '',
      columns: [],
      selectedRowKeys: [],
      goods_id: 0,
      attr: [],
      visible: false,
      attrColumns: attrColumns,
      autoFocusData: { type: '', index: 0 }, // 获取焦点参数
      updateParams: {
        name: '',
        price: null,
        virtual_sales: null,
        goods_num: null,
      },
      isShowSaleConfDrawer: false,
      currentGood: {},
      dataSource: [],
      totalCount: 0,
      pageSize: 10,
      pageNo: 1,
      showNamePopover: false,
      showPricePopover: false,
      showRestPopover: false,
      showVirtualPopover: false,
      showSortPopover: false,
      currentGoodsItem: {},
      pagination: {},
      loading: false,
      showQrCode: false,
      qrcodeUrl: '',

      skuColumns,
      showBind: false,
      currentCloud: {},
    }
  },
  created() {},
  mounted() {
    let columns = [
      {
        title: '商品ID',
        dataIndex: 'id',
        key: 'id',
      },
      {
        title: '产品名称',
        dataIndex: 'name',
        key: 'name',
        width: 100,
        scopedSlots: { customRender: 'goods_name' },
      },
      // {
      //   title: '供货商信息',
      //   width: 150,
      //   scopedSlots: { customRender: 'supplier' },
      // },
      {
        title: '产品类型',
        dataIndex: 'product_type',
        key: 'product_type',
        scopedSlots: { customRender: 'product_type' },
      },
      {
        title: '售价',
        dataIndex: 'price',
        key: 'price',
        defaultSortOrder: '',
        sorter: (a, b) => a.price - b.price,
        scopedSlots: { customRender: 'price' },
      },
      {
        title: '库存',
        dataIndex: 'goods_num',
        key: 'goods_num',
        defaultSortOrder: '',
        sorter: (a, b) => a.goods_num - b.goods_num,
        scopedSlots: { customRender: 'goods_num' },
      },
      {
        title: '虚拟销量',
        dataIndex: 'virtual_sales',
        key: 'virtual_sales',
        defaultSortOrder: '',
        sorter: (a, b) => a.virtual_sales - b.virtual_sales,
        scopedSlots: { customRender: 'virtual_sales' },
      },
      {
        title: '成本价',
        dataIndex: 'cost_price',
        key: 'cost_price',
        sorter: (a, b) => a.cost_price - b.cost_price,
      },
      {
        title: '预估利润',
        dataIndex: 'profit',
        key: 'profit',
        scopedSlots: { customRender: 'profit' },
        width: 100,
      },
      {
        title: '排序',
        dataIndex: 'sort',
        key: 'sort',
        sorter: (a, b) => a.sort - b.sort,
        scopedSlots: { customRender: 'sort' },
      },
      {
        title: '上下架',
        dataIndex: 'status',
        key: 'status',
        scopedSlots: { customRender: 'status' },
      },
      {
        title: '操作',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
      },
    ]

    if (this.type !== 'cloud') {
      columns.splice(3, 1)
    }

    this.columns = columns
  },
  methods: {
    // 绑定和解绑操作
    async leadGood() {
      let goods = JSON.parse(JSON.stringify(this.currentCloud))

      const res = await leadGood({
        id: goods.id,
      })
      if (res.code == 0) {
        this.$message.success('导入成功~')
        this.showBind = false
        this.tablePageChange(1)
      } else {
        this.$message.error(res.msg)
      }
    },

    async updateProduct(id) {
      let res = await supplierGoods({ id })
      if (res.code === 0) {
        let goods_info = res.data.list[0]
        goods_info.attrs = JSON.parse(goods_info.attrs)
        this.currentCloud = goods_info
        this.showBind = true
      } else {
        return null
      }
    },

    // 单个同步商品
    synchronizationGood(id) {
      const that = this
      this.$confirm({
        title: '是否同步该商品?',
        onOk() {
          const hide = that.$message.loading('处理中...')
          synchronizationOutlet({
            goods_id: [id],
          })
            .then((res) => {
              if (res.code === 0) {
                that.$message.success(res.msg)
              } else {
                that.$message.error(res.msg)
              }
            })
            .catch((error) => {
              console.log(error)
            })
            .then(() => {
              hide()
            })
        },
        onCancel() {},
      })
    },
    // 获取商品列表数据
    getGoodsData(reset) {
      var newObj = {}
      if (reset) {
        if (this.queryParam.name) {
          this.pageNo = 1;
        }
      }
      newObj = { ...this.queryParam, status: this.status, pageNo: this.pageNo, pageSize: this.pageSize }
      this.loading = true
      return list(newObj).then((res) => {
        console.log('商品数据', res)
        this.loading = false
        if (!res.code) {
          this.dataSource = res.data.data
          this.totalCount = res.data.totalCount
          this.$emit('onBookGoodsIds', res.book_goods_ids)
        }
      })
    },
    // 分页请求
    tablePageChange(e) {
      console.log(e)
      this.pageNo = e
      this.getGoodsData()
    },
    // 分页器页数修改回调
    paginationSizeChange(current, size) {
      console.log(current, size)
      this.pageNo = current
      this.pageSize = size
      this.getGoodsData()
    },
    // 重置Popover
    resetPopover() {
      this.showNamePopover = false
      this.showPricePopover = false
      this.showRestPopover = false
      this.showVirtualPopover = false
      this.showSortPopover = false
    },
    // 点击修改，弹出Popover
    openPopover(row, index) {
      console.log(row, index)
      this.currentGoodsItem = row
      this.updateParams.name = row.name
      this.updateParams.price = parseFloat(row.price)
      this.updateParams.virtual_sales = Number(row.virtual_sales)
      this.updateParams.goods_num = Number(row.goods_num)
      this.updateParams.sort = Number(row.sort)
      if (index == 0) {
        this.showNamePopover = true
        return
      }
      if (index == 1) {
        this.showPricePopover = true
        return
      }
      if (index == 2) {
        this.showRestPopover = true
        return
      }
      if (index == 4) {
        this.showVirtualPopover = true
        return
      }
      if (index == 5) {
        this.showSortPopover = true
        return
      }
    },
    // 更新编辑的属性
    submitUpdate(row, index) {
      if (!this.updateParams.name) {
        this.$message.warn('商品名称不能为空')
        return
      }
      const params = {
        id: row.id,
        type: '',
        value: '',
      }
      switch (index) {
        case 0:
          params.type = 'name'
          params.value = this.updateParams.name
          break
        case 1:
          params.type = 'price'
          params.value = this.updateParams.price
          break
        case 2:
          params.type = 'goods_num'
          params.value = this.updateParams.goods_num
          break
        case 4:
          params.type = 'virtual_sales'
          params.value = this.updateParams.virtual_sales
          break
        case 5:
          params.type = 'sort'
          params.value = this.updateParams.sort
          break
        default:
          break
      }
      this.resetPopover()
      setAttr(params).then((res) => {
        if (res.code === 0) {
          this.getGoodsData()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 点击编辑属性
    setAttr(goodsId, attr, goods, index) {
      console.log(goods)
      this.currentGoodsItem = goods
      this.attr = JSON.parse(attr)
      this.goods_id = goodsId
      if (goods.use_attr == 1) {
        this.resetPopover()
        this.editAttr(goods)
      } else {
        this.openPopover(goods, index)
      }
    },
    // 修改规格回调
    onAfterClose(type) {
      this.isShowEditAttrModal = false
      this.currentGood = {}
      if (type) {
        this.getGoodsData()
      }
    },
    // 编辑规格
    editAttr(goods) {
      this.currentGood = goods
      this.isShowEditAttrModal = true
    },

    onMouseLeaveTable(e) {
      if (e.relatedTarget && e.relatedTarget.className.indexOf('popover') != -1) {
        return
      }
      this.currentRowId = ''
    },
    // 满减设置
    openDrawer(id, goods) {
      this.currentGood = goods
      this.goodsId = id
      this.isShowSaleConfDrawer = true
    },
    // 复制小程序链接
    copyUrl(id) {
      this.$copyText(`goods/goods/goods?id=${id}`).then(
        () => {
          this.$message.success('复制成功！')
        },
        () => {
          this.$message.error('复制成功！')
        }
      )
    },
    // 小程序二维码
    showQscode(id) {
      const h = this.$createElement
      this.$message.loading({ content: '请求中...', key: 'qrcodeLoading' })
      getQr({
        id,
      })
        .then((res) => {
          if (res.code === 0) {
            console.log(res)
            const qrcodeUrl = res.data.qr_url
            // this.$info({
            //   title: '小程序二维码',
            //   content: h('img', {
            //     attrs: {
            //       src: qrcodeUrl,
            //     },
            //     style: {
            //       width: '100%',
            //       margin: '10px auto',
            //     },
            //   }),
            // })
            this.showQrCode = true
            this.qrcodeUrl = qrcodeUrl
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch((error) => {
          console.log(error)
        })
        .then(() => {
          this.$message.destroy()
        })
    },
    // 刷新数据
    refresh() {
      this.getGoodsData(true)
    },
    // 操作编辑
    onEdit(id) {
      if (id > 0) {
        this.$router.push({ name: 'GoodsEdit', query: { id: id, type: 'cloud' } })
      } else {
        this.$router.push({ name: 'GoodsEdit' })
      }
    },
    // 表格选中
    onSelectChange(selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    // 删除数据
    delGoodById(id) {
      this.$confirm({
        title: '是否删除该条数据?',
        onOk: () => {
          updateAllApi({
            type: 'is_delete',
            status: 1,
            goods_id: [id],
          }).then((res) => {
            if (res.code === 0) {
              this.getGoodsData()
            } else {
              this.$message.error(res.msg)
            }
          })
        },
        onCancel() {},
      })
    },
    // 批量操作
    updateAll(type, status) {
      if (!this.selectedRowKeys.length) {
        this.$message.warn('请先勾选商品')
        return
      }
      var text = '删除'
      if (type === 'status') {
        text = status === 1 ? '上架' : '下架'
      } else if (type === 'quick_purchase') {
        text = status === 1 ? '加入快速购买' : '关闭快速购买'
      }
      this.$confirm({
        title: '是否批量' + text + '?',
        onOk: () => {
          updateAllApi({
            type: type,
            status: status,
            goods_id: this.selectedRowKeys,
          }).then((res) => {
            if (res.code === 0) {
              this.refresh()
            } else {
              this.$message.error(res.msg)
            }
          })
        },
        onCancel() {},
      })
    },
    // 上下架
    onConfirmStatus(type, goodsId) {
      status({ id: goodsId, type: type }).then((res) => {
        if (res.code === 0) {
          this.refresh()
          this.$message.success(res.msg)
        } else {
          this.refresh()
          this.$message.error(res.msg)
        }
      })
    },

    updateParam(value, type) {
      console.log(value)
      this.queryParam[type] = value
    },

    handleOk() {
      this.$message.loading({ content: '提交中', key: 'loading' })
      setAttrNum({
        attr: this.attr,
        id: this.goods_id,
      }).then((res) => {
        if (res.code === 0) {
          this.visible = false
          this.refresh()
          this.$message.success({ content: res.msg, key: 'loading' })
        } else {
          this.$message.error({ content: res.msg, key: 'loading' })
        }
        this.resetPopover()
      })
    },
    /**
     * 修改规格数据
     */
    handleChange(value, type, index) {
      this.autoFocusData = {
        type: type,
        index: index,
      }
      this.attr[index][type] = value === null ? '' : value
    },
    settingAll(e, setSelectedKeys, selectedKeys) {
      this.attr.forEach((element, index) => {
        this.attr[index][setSelectedKeys.column.dataIndex] = e
      })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-table-body {
  min-height: 360px;
}
.slotTable {
  /deep/.ant-table-row-cell-break-word {
    padding: 2px 8px !important;
  }
  /deep/.ant-table-body {
    min-height: 0px;
  }
}

/deep/.ant-table-body::-webkit-scrollbar {
  display: none; /*ChromeSafari*/
}

.of_el {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.table_operator_icon {
  font-size: 18px;
  padding: 0 5px;
  color: rgb(182, 182, 182);
  cursor: pointer;
  &:hover {
    color: rgb(24, 144, 255);
  }
}

.goods-info {
  display: flex;
  align-items: flex-start;
  position: relative;
  width: 250px;
  .goods-pic {
    width: 48px;
    height: 48px;
    border-radius: 4px;
    display: block;
  }
  .info-box {
    margin-left: 24px;
    margin-right: 5px;
    flex: 1;
    overflow: hidden;
    .goods-name {
      width: 125px;
      font-size: 14px;
      line-height: 22px;
      color: rgba(0, 0, 0, 0.65);
      display: -webkit-box;
      white-space: normal;
      text-overflow: ellipsis;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
      word-break: break-all;
      overflow: hidden;
    }
  }
  .item-edit-box {
    width: 15px;
    height: 100%;
    position: absolute;
    right: -12px;
    top: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}
.goods-price-row {
  position: relative;
  display: inline-block;
  .row-price-edit {
    position: absolute;
    right: -27px;
    top: 0;
    width: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.goods-rest-row {
  position: relative;
  display: inline-block;
  .row-rest-edit {
    position: absolute;
    right: -27px;
    top: 0;
    width: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.goods-virtual-row {
  position: relative;
  display: inline-block;
  .row-virtual-edit {
    position: absolute;
    right: -27px;
    top: 0;
    width: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.goods-type {
  min-width: 60px;
  display: inline-block;
}
.table-action {
  min-width: 93px;
  display: inline-block;
}
</style>
